# 用于创建和管理插件的分页-分组-按钮集合
class_name plugPages

var tab:TabContainer # 最外层的选项卡容器
var pages:mPages = mPages.new()# 分页集合


func _init():
	tab = TabContainer.new()
	tab.tab_align = 0
	tab.rect_position = Vector2(0,0)
	# 布局 - 整个矩形
	tab.anchor_top = 0
	tab.anchor_left = 0
	tab.anchor_right = 1
	tab.anchor_bottom = 1
	
	pages.tab = tab # 将tab传递给分页合集pages

class mPages: # 分页集合
	var tab:TabContainer
	var pgs = []
	
	func add(title:String) -> Page: # 添加
		var page = Page.new()
		page.title = title
		page.tab = tab
		# 添加page对象到集合
		pgs.append(page)
		# 实际添加子控件到tab
		tab.add_child(page.scroll)
		return page

	func remove(index:int) -> void: # 删除
		# 实际从tab删除分页下的子控件
		tab.remove_child(tab.get_children()[index])
		# 从集合中删除
		pgs.remove(index)

	func items(index:int) -> Page: # 返回相应分页的引用
		return pgs[index]

	func count() -> int: # 返回页数
		return pgs.size()

# ===== 选项卡页面 ===== 
class Page: 
	var tab:TabContainer
	var title:String setget set_title # 标题
	var scroll:ScrollContainer # 外层容器
	var groups:Groups = Groups.new()
	
	
	func _init():
		# 外层滚动容器
		scroll = ScrollContainer.new()
		# VBox - 纵向排列分组和其他功能部分
		var vbox:VBoxContainer = VBoxContainer.new()
		vbox.size_flags_horizontal = 3
		scroll.add_child(vbox)
		# 传递VBox
		groups.vbox = vbox

	func set_title(value):
		scroll.name = value


class Groups: # 分组集合
	var vbox:VBoxContainer
	var gups = []
	
	func add(title:String) -> Group: # 添加
		var gup:Group = Group.new()
		gup.title = title
		gup.connect("btn_click",self,"on_btn_click")
		# 添加gup对象到集合
		gups.append(gup)
		# 实际添加子控件到tab
		vbox.add_child(gup.gupBtn)
		vbox.add_child(gup.gupGrid)
		return gup

	func remove(index:int) -> void: # 删除
		var children = vbox.get_children()
		var gupBtns = []
		var gupGrids = []
		for child in children:
			if child is Button:
				gupBtns.append(child)
			elif child is GridContainer:
				gupGrids.append(child)
		# 实际从vbox删除分组按钮
		vbox.remove_child(gupBtns[index])
		vbox.remove_child(gupGrids[index])
		# 从集合中删除Group对象
		gups.remove(index)
		

	func items(index:int) -> Group: # 返回相应分页的引用
		return gups[index]

	func count() -> int: # 返回页数
		return gups.size()

class Group: # 分组
	
	var vbox:VBoxContainer
	var title:String setget set_title # 标题
	var gupBtn:Button
	var gupGrid:GridContainer
	var btns:Btns = Btns.new()

	func _init():
		# 分组按钮
		gupBtn = Button.new() 
		gupBtn.flat = true # 平面化按钮
		gupBtn.size_flags_horizontal = 3
		# 分组下按钮容器
		gupGrid = GridContainer.new()
		
		gupGrid.columns = 2 # 两列
		gupGrid.set("custom_constants/vseparation",10) # 水平间隔
		gupGrid.set("custom_constants/hseparation",10) # 垂直间隔
		gupGrid.set("size_flags_horizontal",3)
		# 向下传递网格容器
		btns.grid = gupGrid
#		gupBtn.connect("pressed",self,"gup_btn_click",[gupBtn.text])
	
	func set_title(value):
		gupBtn.text = value
	pass

class Btns: # 单个分组下的按钮集合
	
	var grid:GridContainer
	var bts = []
	
	func add(title:String) -> Btn: # 添加
		var btn = Btn.new()
		btn.title = title
		# 添加btn对象到集合
		bts.append(btn)
		# 实际添加子控件到grid
		grid.add_child(btn.mbtn)
		
		return btn
	
	

class Btn: # 具体的按钮
	signal btn_click(title,data) # 具体按钮的单击事件
	var grid:GridContainer
	var title:String setget set_title # 标题
	var mbtn:Button
	var data:Dictionary # 按钮所需加载的其他信息放入此字典
	
	func _init():
		# 具体的节点按钮
		mbtn = Button.new() 
		mbtn.set("size_flags_horizontal",3)
		
	func set_title(value):
		mbtn.text = value
		mbtn.connect("pressed",self,"on_btn_click",[title])
		
	func on_btn_click(title:String):
		emit_signal("btn_click",title,data)

